package com.chen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.dto.DishDto;
import com.chen.entity.R;
import com.chen.service.DishFlavorService;
import com.chen.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 添加
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R save(@RequestBody DishDto dishDto) {
        dishService.saveDishAndFlavors(dishDto);
        return R.success(null);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(int page, int pageSize, String name) {
        Page<DishDto> dishDtos = dishService.page1(page, pageSize, name);
        return R.success(dishDtos);
    }

    /**
     * 修改状态
     *
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    /*public R update(@RequestParam List<Long>ids, @PathVariable int status) {
        dishService.up(ids, status);
        return R.success(null);
    }*/
    public R update(long[] ids, @PathVariable int status) {
        dishService.up(ids, status);
        return R.success(null);
    }

    /**
     * 修改的数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R findByid(@PathVariable Long id) {
        DishDto byId = dishService.findById(id);
        return R.success(byId);
    }


    @PutMapping
    public R upadet(@RequestBody DishDto dishDto) {
        dishService.updates(dishDto);
        return R.success(null);
    }

    @DeleteMapping
    private R delete(Long[] ids) {
        dishService.delete(ids);
        return R.success(null);
    }

    //根据分来ID查询对应的菜品信息
    @GetMapping("/list")
    public R list(long categoryId,Integer status) {
        List<DishDto> dishlist = dishService.getList(categoryId,status);
        return R.success(dishlist);
    }


}
